package com.acme.gprs.webservice;

import javax.annotation.PreDestroy;
import javax.jws.WebService;

import com.acme.gprs.server.GPRSAppliancesServer;
import com.acme.gprs.server.GPRSCommand;

@WebService(endpointInterface="com.acme.gprs.webservice.GPRSDashboard")
public class GPRSDashboardImpl implements GPRSDashboard{
	private GPRSAppliancesServer gprsServer = GPRSAppliancesServer.getInstance();
	
	@Override
	public String openPort(String imei, int port) {
		String result = null;
		if( port > 0 && port < 7 ){
			String speed = "0";
			char[] newPortsStatus = new char[]{'2','2','2','2','2'};
			newPortsStatus[port == 6?4:port-1] = '0';
			String data[] = new String[]{speed, new String(newPortsStatus),};
			result = gprsServer.sendCommand(imei, GPRSCommand.OPEN_PORT, data);
		}else{
			result = "Port parameter is invalid";
		}
		return result;
	}

	@Override
	public String closePort(String imei, int port) {
		String result = null;
		if( port > 0 && port < 7 ){
			String speed = "0";
			char[] newPortsStatus = new char[]{'2','2','2','2','2'};
			newPortsStatus[port == 6?4:port-1] = '1';
			String data[] = new String[]{speed, new String(newPortsStatus)};
			result = gprsServer.sendCommand(imei, GPRSCommand.CLOSE_PORT, data);;
		}else{
			result = "Port parameter is invalid";
		}
		return result;
	}
	
	@Override
	public String statusPorts(String imei) {
		return gprsServer.sendCommand(imei, GPRSCommand.STATUS_PORT, null);
	}

	@Override
	public String getLocation(String imei) {
		return gprsServer.sendCommand(imei, GPRSCommand.TRACK_ON_DEMAND, null);
	}
	
	@PreDestroy
	public void release(){
		gprsServer.shutdown();
	}

	@Override
	public String getModel(String imei) {
		return "T1-600";
	}

	@Override
	public String statusDevice(String imei) {
		return gprsServer.statusDevice(imei);
	}
}